package com.ruoyi.pos.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pos.domain.system.PosGroup;
import com.ruoyi.pos.mapper.system.PosGroupMapper;
import com.ruoyi.pos.service.system.IPosGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * POS柜组管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-30
 */
@Api(tags = "POS柜组管理")
@RestController
@RequestMapping("/pos/group")
public class PosGroupController extends BaseController {

    @Autowired
    private IPosGroupService posGroupService;
    
    @Autowired
    private PosGroupMapper posGroupMapper;

    /**
     * 查询柜组列表
     */
    @ApiOperation("查询柜组列表")
    @PreAuthorize("@ss.hasPermi('pos:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(PosGroup posGroup) {
        startPage();
        List<PosGroup> list = posGroupMapper.selectPosGroupList(posGroup);
        return getDataTable(list);
    }

    /**
     * 导出柜组列表
     */
    @ApiOperation("导出柜组列表")
    @PreAuthorize("@ss.hasPermi('pos:group:export')")
    @Log(title = "POS柜组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PosGroup posGroup) {
        List<PosGroup> list = posGroupMapper.selectPosGroupList(posGroup);
        ExcelUtil<PosGroup> util = new ExcelUtil<PosGroup>(PosGroup.class);
        util.exportExcel(response, list, "柜组数据");
    }

    /**
     * 获取柜组详细信息
     */
    @ApiOperation("获取柜组详细信息")
    @PreAuthorize("@ss.hasPermi('pos:group:query')")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@ApiParam("柜组ID") @PathVariable Long groupId) {
        return success(posGroupMapper.selectPosGroupById(groupId));
    }

    /**
     * 新增柜组
     */
    @ApiOperation("新增柜组")
    @PreAuthorize("@ss.hasPermi('pos:group:add')")
    @Log(title = "POS柜组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PosGroup posGroup) {
        return toAjax(posGroupMapper.insertPosGroup(posGroup));
    }

    /**
     * 修改柜组
     */
    @ApiOperation("修改柜组")
    @PreAuthorize("@ss.hasPermi('pos:group:edit')")
    @Log(title = "POS柜组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosGroup posGroup) {
        return toAjax(posGroupMapper.updatePosGroup(posGroup));
    }

    /**
     * 删除柜组
     */
    @ApiOperation("删除柜组")
    @PreAuthorize("@ss.hasPermi('pos:group:remove')")
    @Log(title = "POS柜组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@ApiParam("柜组ID数组") @PathVariable Long[] groupIds) {
        return toAjax(posGroupMapper.deletePosGroupByIds(groupIds));
    }

    /**
     * 构建柜组树
     */
    @ApiOperation("构建柜组树")
    @GetMapping("/tree")
    public AjaxResult tree() {
        try {
            return success(posGroupService.buildGroupTree());
        } catch (Exception e) {
            return error("构建柜组树失败：" + e.getMessage());
        }
    }

    /**
     * 查询柜组下拉树结构
     */
    @ApiOperation("查询柜组下拉树结构")
    @GetMapping("/treeselect")
    public AjaxResult treeselect() {
        try {
            return success(posGroupService.buildGroupTree());
        } catch (Exception e) {
            return error("查询柜组树失败：" + e.getMessage());
        }
    }

    /**
     * 根据角色ID查询柜组树结构
     */
    @ApiOperation("根据角色ID查询柜组树结构")
    @GetMapping("/roleGroupTreeselect/{roleId}")
    public AjaxResult roleGroupTreeselect(@ApiParam("角色ID") @PathVariable Long roleId) {
        try {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("checkedKeys", new Long[]{});  
            ajax.put("groups", posGroupService.buildGroupTree());
            return ajax;
        } catch (Exception e) {
            return error("查询角色柜组树失败：" + e.getMessage());
        }
    }

    /**
     * 获取柜组关联的POS设备
     */
    @ApiOperation("获取柜组关联的POS设备")
    @PreAuthorize("@ss.hasPermi('pos:group:query')")
    @GetMapping("/devices/{groupId}")
    public AjaxResult getGroupDevices(@ApiParam("柜组ID") @PathVariable Long groupId) {
        // TODO: 实现获取柜组设备列表
        return success(new java.util.ArrayList<>());
    }

    /**
     * 获取柜组关联的收银员
     */
    @ApiOperation("获取柜组关联的收银员")
    @PreAuthorize("@ss.hasPermi('pos:group:query')")
    @GetMapping("/cashiers/{groupId}")
    public AjaxResult getGroupCashiers(@ApiParam("柜组ID") @PathVariable Long groupId) {
        // TODO: 实现获取柜组收银员列表
        return success(new java.util.ArrayList<>());
    }

    /**
     * 设置柜组关联的POS设备
     */
    @ApiOperation("设置柜组关联的POS设备")
    @PreAuthorize("@ss.hasPermi('pos:group:edit')")
    @Log(title = "柜组设备关联", businessType = BusinessType.UPDATE)
    @PostMapping("/devices/{groupId}")
    public AjaxResult setGroupDevices(@ApiParam("柜组ID") @PathVariable Long groupId, 
                                      @RequestBody java.util.Map<String, Object> params) {
        // TODO: 实现设置柜组设备关联
        return success("设置成功");
    }

    /**
     * 设置柜组关联的收银员
     */
    @ApiOperation("设置柜组关联的收银员")
    @PreAuthorize("@ss.hasPermi('pos:group:edit')")
    @Log(title = "柜组收银员关联", businessType = BusinessType.UPDATE)
    @PostMapping("/cashiers/{groupId}")
    public AjaxResult setGroupCashiers(@ApiParam("柜组ID") @PathVariable Long groupId, 
                                       @RequestBody java.util.Map<String, Object> params) {
        // TODO: 实现设置柜组收银员关联
        return success("设置成功");
    }
}
